package edu.seu.mart.controller;

import edu.seu.mart.model.*;
import edu.seu.mart.service.ICommentService;

import edu.seu.mart.service.IOrderService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Created by Administrator on 2015/3/24.
 */
@Controller
@RequestMapping("/comment")
public class CommentController {

    @Resource
    private ICommentService commentService;
    @Resource
    private IOrderService orderService;

    private List reviewGoods =new ArrayList();
    @RequestMapping(value = "/addComment", method = RequestMethod.GET)
    public String addComment(@ModelAttribute("comment") Comment comment) {
        return "user/comment";
    }

    @RequestMapping(value = "/addComment", method = RequestMethod.POST)
    public String addComment(@RequestParam("orderId") String orderId,@RequestParam("goodsId") String goodsId,@RequestParam("review") String review,@RequestParam("content") String content,Model model,HttpSession session) {
        Comment comment=new Comment();
        System.out.println("id:"+goodsId);
        comment.setGoodsId(Integer.parseInt(goodsId));
        comment.setReview(Integer.parseInt(review));
        comment.setContent(content);
        User user = (User) session.getAttribute("loginUser");
        comment.setUser(user);
        comment.setCommentTime(new Date());
        commentService.add(comment);
        reviewGoods.add(goodsId);
        System.out.println("id:"+reviewGoods);
        Order order = orderService.load(Integer.parseInt(orderId));
        Set<OrderItem> orderItems = new HashSet<OrderItem>();
        orderItems = order.getOrderItems();
        List<OrderItem> list= new ArrayList<OrderItem>();
        for(OrderItem item:orderItems){
            list.add(item);
        }
        System.out.println("p"+list.size());
        List<OrderItem> List1= new ArrayList<OrderItem>();
        int i=0;
        for(int j=0;j<reviewGoods.size();j++){
            for(int m=0;m<list.size();m++) {
                if (Integer.parseInt(reviewGoods.get(j).toString())==(list.get(m).getGoods().getId())) {
                    List1.add(i, list.get(m));
                    i++;
                }
            }
        }
        list.removeAll(List1);
        System.out.println("****"+list.size());
        if(list.size()==0) {
            orderService.updateState(Integer.parseInt(orderId), "已评价");
            throw new MartException("订单评论成功");
        }
        else {
            model.addAttribute("orderId", orderId);
            model.addAttribute("order", list);
            return "user/comment";
        }
    }


    @RequestMapping(value = "/commentList", method = RequestMethod.GET)
    public String commentList(@RequestParam("goodsId") String goodsId, Model model) {
        model.addAttribute("commentList", commentService.findCommentByGoods(Integer.valueOf(goodsId)));
        return null;
    }
}
